package com.atguigu.servlet;

import com.atguigu.bean.Book;
import com.atguigu.bean.Cart;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Date:2020/9/15
 * Author:ybc
 * Description:
 */
public class CartServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    /**
     * 添加图书信息到购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addBook2Cart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String bookId = request.getParameter("bookId");
        //通过bookId获取图书信息
        Book book = bookService.getBookByBookId(Integer.parseInt(bookId));
        //获取session对象
        HttpSession session = request.getSession();
        //获取session中的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //判断当前添加图书信息的操作是否为第一次添加
        if(cart == null){
            cart = new Cart();
        }
        //将图书信息添加到购物车中
        cart.addBook2Cart(book);
        //将购物车对象放在session中进行共享
        session.setAttribute("cart", cart);
        //同时将新添加的图书的书名放在session中共享
        session.setAttribute("bookTitle", book.getTitle());
        //通过重定向跳转到来源页面
        response.sendRedirect(request.getHeader("referer"));
    }

    /**
     * 删除购物项
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteCartItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String bookId = request.getParameter("bookId");
        //获取session对象
        HttpSession session = request.getSession();
        //获取存储在session中的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //删除购物项
        cart.deleteCartItem(bookId);
        //重定向到购物车页面cart.jsp
        response.sendRedirect(request.getHeader("referer"));
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //获取存储在session中的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //清空购物车
        cart.clearCart();
        //重定向到来源页面
        response.sendRedirect(request.getHeader("referer"));
    }

    /**
     * 更新购物项的数量
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String bookId = request.getParameter("bookId");
        String countStr = request.getParameter("count");
        //获取session
        HttpSession session = request.getSession();
        //获取存储在session中的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //更新购物项的数量
        cart.updateCount(bookId, countStr);
        //重定向到来源页面
        response.sendRedirect(request.getHeader("referer"));
    }
}
